package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Collect;
import com.abaaba.project2.bean.CollectExample;
import com.abaaba.project2.bean.vo.AdminCollectVo;
import com.abaaba.project2.mapper.CollectMapper;
import com.abaaba.project2.service.AdminCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminCollectServiceImpl implements AdminCollectService {
    @Autowired
    CollectMapper collectMapper;

    @Override
    public AdminCollectVo getCollectList(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page, limit);
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(sort + " " + order);
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();
        AdminCollectVo adminCollectVo = new AdminCollectVo();
        adminCollectVo.setItems(collects);
        adminCollectVo.setTotal(total);
        return adminCollectVo;
    }
}
